#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>

using namespace std;

std::string getValueOfTag1(std::string filepath, std::string &exifDataName);
std::string getValueOfTag2(std::string filepath, std::string &exifDataName);

/*the information we can get, see more http://www.exiv2.org/tags.html */
//Exif.Photo.FocalLength
//Exif.Image.GPSTag                   0x8825 Long        1  662
//Exif.GPSInfo.GPSAltitudeRef         0x0005 Rational    1  200/100
//Exif.GPSInfo.GPSLatitudeRef         0x0001 Ascii       2  N
//Exif.GPSInfo.GPSLatitude            0x0002 Rational    3  36/1 15/1 224240/10000
//Exif.GPSInfo.GPSLongitudeRef        0x0003 Ascii       2  E
//Exif.GPSInfo.GPSLongitude           0x0004 Rational    3  117/1 6/1 216450/10000
//Exif.GPSInfo.GPSAltitudeRef         0x0005 Byte        1  0
//Exif.GPSInfo.GPSAltitude            0x0006 Rational    1  1524000/1000
//Exif.GPSInfo.GPSTimeStamp           0x0007 Rational    3  22/1 53/1 7/1
//Exif.GPSInfo.GPSProcessingMethod    0x001b Ascii      12  ASCII
//Exif.GPSInfo.GPSDateStamp           0x001d Ascii      11  2016:11:11



int main()
{
    //the path of the image
    std::string filepath = "/home/xiaoguo/Codes/c++practics/gps/img.jpg";
    //the information wo want
    string value = "Exif.Photo.FocalLength";

    //method one
    getValueOfTag1(filepath, value);
    //method two
    getValueOfTag2(filepath, value);

    return 0;
}

std::string getValueOfTag1(std::string filepath, std::string &exifDataName)
{
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filepath);

    if(image.get() == 0)
        cout<<"failed to opened..."<<endl;

    image->readMetadata();
    Exiv2::ExifData &exifData = image->exifData();
    if (exifData.empty()) {
        std::cout<<"empty......"<<endl;
    }

    Exiv2::Exifdatum& Modle_tag = exifData[exifDataName];
    std::string valueOfTag = Modle_tag.toString();
    std::cout<<exifDataName<<" is "<<valueOfTag<<std::endl;

    return valueOfTag;
}

std::string getValueOfTag2(std::string filepath, std::string &exifDataName)
{
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filepath);

    if(image.get() == 0)
        cout<<"failed to opened..."<<endl;

    image->readMetadata();
    Exiv2::ExifData &exifData = image->exifData();
    if (exifData.empty()) {
        std::cout<<"empty......"<<endl;
    }

    Exiv2::ExifKey key(exifDataName);
    Exiv2::ExifData::const_iterator i = exifData.findKey(key);

    std::cout << "key is "<< i->key() <<std::endl;//the name of key
//    std::cout << "Tag(hex) is "<< "0x" << std::hex << i->tag() << std::endl;
//    std::cout << "Type is "<< i->typeName() << std::endl;
//    std::cout << "Tag(dec) is "<< std::dec << i->count() <<std::endl;
//    std::cout << "Value of tag is "<< std::dec << i->value()<<std::endl;
    std::cout << "Value of tag is "<< i->value()<<std::endl;

    string valueOfTag = "yeah!";
    return valueOfTag;
}
